/*
 * 软件版权: 恒生电子股份有限公司
 * 修改记录:
 * 修改日期     修改人员  修改说明
 * ========    =======  ============================================
 * 2021/4/14  lisy31662  新增
 * ========    =======  ============================================
 */
package com.sansi.pencilbook.controller;

import com.sansi.pencilbook.DO.ResponseDO;
import com.sansi.pencilbook.DO.Result;
import com.sansi.pencilbook.Enum.BehaviorEnum;
import com.sansi.pencilbook.entity.User;
import com.sansi.pencilbook.entity.UserBook;
import com.sansi.pencilbook.service.UserBookServer;
import com.sansi.pencilbook.service.UserServer;
import com.sansi.pencilbook.util.ResponseDOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * 功能说明:
 *
 * @author lisy31662
 */
@Slf4j
@RestController
@RequestMapping("/api/userBehavior")
public class UserBehaviorController extends VerifyController implements BaseController {

  @Autowired
  private UserBookServer userBookServer;

  @Autowired
  private UserServer userServer;

  @PostMapping("/u/addBehavior")
  public ResponseDO addBehavior(HttpServletRequest request, @RequestBody UserBook userBook){
    String userAccount = "";
    if (!"".equals(userAccount=getUserAccount(request))) {
      User user = (User) userServer.getUserByAccount(userAccount).getData();
      Calendar calendar = Calendar.getInstance();
      userBook.setTime(new SimpleDateFormat("yyyyMMddHHmm").format(calendar.getTime()));
      userBook.setUserId(user.getUserId());
      userBook.setBehavior(BehaviorEnum.valueOf(userBook.getBehavior()).getBehavior());
      Result result = userBookServer.addUserBook(userBook);
      return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
    }else {
      return ResponseDOUtil.setResponseDO(false, "未登录");
    }
  }

}
